package org.bugjail.servlet.form.edit;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bugjail.data.SimpleTestCase;
import org.bugjail.data.TestCase;
import org.bugjail.dataaccess.DAOFactory;
import org.bugjail.servlet.BasicUIServlet;

/**
 * Servlet implementation class for Servlet: NewSuiteFormServlet
 * 
 */
public class EditSimpleTestCaseFormServlet extends BasicUIServlet {

	static final long serialVersionUID = 1L;
	
	private SimpleTestCase tc;

	@Override
	protected void write(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		//int suiteid = Integer.parseInt(request
		//		.getParameter("suiteid"));

		PrintWriter pw = response.getWriter();

		pw.println("<form method='post' action='EditSimpleTestCaseServlet'>");
		pw.println("Test case name:<br><input type='text' name='name' maxlength='50' value='"+tc.getName()+"' /><br>");
		pw.println("Objective:<br><textarea name='objective'>"+tc.getObjective()+"</textarea><br>");
		pw.println("<input type=hidden name='tcid' value='"
				+ tc.getId() + "' />");
		
		//SEVERITY
		pw.println("Severity: ");
		pw.println("<input type='radio' name='severity' value='"+TestCase.SEVERITY_0+"' "+severityChecked(TestCase.SEVERITY_0, tc.getSeverity())+" ><img src='images/priority01.png'/></input>");
		pw.println("<input type='radio' name='severity' value='"+TestCase.SEVERITY_1+"' "+severityChecked(TestCase.SEVERITY_1, tc.getSeverity())+" ><img src='images/priority02.png'/></input>");
		pw.println("<input type='radio' name='severity' value='"+TestCase.SEVERITY_2+"' "+severityChecked(TestCase.SEVERITY_2, tc.getSeverity())+" ><img src='images/priority03.png'/></input>");
		pw.println("<input type='radio' name='severity' value='"+TestCase.SEVERITY_3+"' "+severityChecked(TestCase.SEVERITY_3, tc.getSeverity())+" ><img src='images/priority04.png'/></input>");
		pw.println("<input type='radio' name='severity' value='"+TestCase.SEVERITY_4+"' "+severityChecked(TestCase.SEVERITY_4, tc.getSeverity())+" ><img src='images/priority05.png'/></input>");
		
		pw.println("<input type=submit value='Update' />");
		pw.println("&nbsp;<a href='TestCaseServlet?suiteid="+tc.getSuite().getId()+"'>Cancel</a>");
		pw.println("</form>");
	}

	private String severityChecked(int severity, int tcseverity) {
		if (severity == tcseverity) {
			return "checked=true";
		} else {
			return "";
		}
	}
	
	@Override
	protected String pageTitle() {
		return "Edit simple test case";
	}

	@Override
	protected void beforeAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int tcid = Integer.parseInt(request.getParameter("tcid"));
		tc = (SimpleTestCase)DAOFactory.getDAOFactory().getTestCaseDAO().retrieveById(tcid);
	}
}